/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.serialization;

import org.newdawn.slick.geom.Vector2f;
import org.simpleframework.xml.convert.Converter;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.OutputNode;

/**
 * Simple XML converter for the Slick2D Vector2f class.
 *
 * @author Matt v.d. Westhuizen
 */
public class Vector2fConverter implements Converter<Vector2f> {
    public Vector2f read(InputNode node) throws Exception {
        float x = 0.0f;
        float y = 0.0f;

        InputNode n = null;
        n = node.getNext();
        while (n != null) {
            if (n.getName().equals("x")) {
                x = Float.parseFloat(n.getValue());
            } else if (n.getName().equals("y")) {
                y = Float.parseFloat(n.getValue());
            }

            n = node.getNext();
        }

        return(new Vector2f(x, y));
    }

    public void write(OutputNode node, Vector2f v) throws Exception {
        OutputNode n = node.getChild("x");
        n.setValue(Float.toString(v.x));

        n = node.getChild("y");
        n.setValue(Float.toString(v.y));

        node.commit();
    }
}
